package com.origin.learn.es.util;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Date: 2020/2/19
 * @Time: 21:12
 * @Created by:   dingtao
 * @Modified by:
 * @Modified at:
 */
public class ESConnection {
    public static void main(String[] args) throws Exception{
        RestClientBuilder builder = RestClient.builder(HttpHost.create("localhost:9200"));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        GetRequest request = new GetRequest("megacorp", "2");
        RequestOptions requestOptions = RequestOptions.DEFAULT;
        GetResponse response = client.get(request, requestOptions);
        Map<String, Object> source = response.getSource();
        int x= 1;
        IndexRequest indexRequest = new IndexRequest("goods", "apple");
        Map<String, Object> map = new HashMap<>();
        map.put("name", "小明");
        map.put("age", 16);
        map.put("price", 2000.5);
        indexRequest.source(map);
        client.index(indexRequest, requestOptions);


//        indexRequest.source()

    }



//    public Object countQuery(RestHighLevelClient client){
////        Node node1 = new Node(null);
////        node1.client().prepareSearch()
////                .addAggregation(AggregationBuilders.terms("by_country").field("country")
//////                .subAggregation(A))
//////        AggregationBuilders.terms()
////        AggregationBuilder aggregationBuilder = AggregationBuilders.count("ageCount").field("age");
////        RangeQueryBuilder ramgeQueryBuilder = QueryBuilders.rangeQuery("age").from(30, true)
////                .to(30, true);
////        return null;
////        QueryBuilder queryBuilder = client
//    }
}
